37 Lecture

CS201

Midterm & Final Term Short Notes

Overloading Insertion and Extraction Operators

In C++, the insertion (<<) and extraction (>>) operators can be overloaded to enable custom input and output of user-defined types. Overloading these operators allows objects of a user-defined type to be input or output using the same syntax as


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which operator is used for input in C++? a) << b) >> c) & d) *

Answer: b) >>

  1. Which operator is used for output in C++? a) + b) * c) >> d) <<

Answer: d) <<

  1. Which function is used to overload the extraction operator? a) operator- b) operator>> c) operator< d) operator[]

Answer: b) operator>>

  1. Which function is used to overload the insertion operator? a) operator< b) operator>> c) operator- d) operator<<

Answer: d) operator<<

  1. What is the return type of the overloaded insertion operator? a) void b) int c) ostream& d) istream&

Answer: c) ostream&

  1. What is the return type of the overloaded extraction operator? a) void b) int c) ostream& d) istream&

Answer: d) istream&

  1. What is the first parameter of the overloaded insertion operator? a) ostream& b) istream& c) int d) char

Answer: a) ostream&

  1. What is the first parameter of the overloaded extraction operator? a) ostream& b) istream& c) int d) char

Answer: b) istream&

  1. What is the second parameter of the overloaded insertion operator? a) ostream& b) istream& c) int d) const Object&

Answer: d) const Object&

  1. What is the second parameter of the overloaded extraction operator? a) ostream& b) istream& c) int d) Object&

Answer: d) Object&



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the purpose of overloading insertion and extraction operators? Answer: Overloading these operators enables custom input and output of user-defined types.

  2. How do you overload the insertion operator in C++? Answer: The insertion operator is overloaded using the syntax: ostream& operator<<(ostream& os, const Object& obj)

  3. How do you overload the extraction operator in C++? Answer: The extraction operator is overloaded using the syntax: istream& operator>>(istream& is, Object& obj)

  4. What is the return type of the overloaded insertion operator? Answer: The return type of the overloaded insertion operator is ostream&.

  5. What is the return type of the overloaded extraction operator? Answer: The return type of the overloaded extraction operator is istream&.

  6. How do you define an insertion operator for a class in C++? Answer: You define an insertion operator for a class in C++ by declaring a friend function of the class.

  7. How do you define an extraction operator for a class in C++? Answer: You define an extraction operator for a class in C++ by declaring a friend function of the class.

  8. Can you overload the insertion and extraction operators for built-in types in C++? Answer: No, these operators cannot be overloaded for built-in types in C++.

  9. What is the purpose of the 'const' keyword in the insertion operator's parameter list? Answer: The 'const' keyword in the insertion operator's parameter list specifies that the object being inserted should not be modified.

  10. What is the purpose of the '&' symbol in the insertion and extraction operator's parameter list? Answer: The '&' symbol in the insertion and extraction operator's parameter list specifies that the parameters should be passed by reference.

In C++, the insertion (<<) and extraction (>>) operators are used for input and output operations respectively. These operators can be overloaded to allow for custom input and output of user-defined types. To overload the insertion operator for a class, you declare a friend function that takes an output stream object (ostream&) and a constant reference to the object being inserted (const Object&). The function should return a reference to the output stream object. Similarly, to overload the extraction operator, you declare a friend function that takes an input stream object (istream&) and a reference to the object being extracted (Object&). The function should return a reference to the input stream object. When overloading these operators, it is important to handle any errors or exceptions that may occur during input or output operations. It is also important to ensure that the input and output operations are performed correctly and efficiently. One common technique used when overloading these operators is to define them as non-member functions, so that they can be used with objects of different classes. This can be done by defining the functions outside of the class definition, and declaring them as friend functions of the class. Overloading these operators can make input and output operations for user-defined types more convenient and intuitive. It also allows for greater control over the formatting and appearance of the output, which can be useful in certain applications. However, it should be used judiciously, as overloading these operators can make code more difficult to read and understand if overused or used improperly.